package com.metaera.app.ui.activities;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.data.model.AppConfigResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ToastUtils;

public class SocialMediaActivity extends BaseActivity {

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_social_media;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ActivityTitleHelper.setTitle(this, R.string.me_social_media);

        // 一些配置
        AppConfigResponse config = LocalConfigStore.getInstance().getConfig();
        if (config != null) {
            findViewById(R.id.layout_twitter).setOnClickListener(v -> // twitter
                    openBrowser(v.getContext(), config.getTwitter().getValue()));

            findViewById(R.id.layout_telegram).setOnClickListener(v -> // telegram
                    openBrowser(v.getContext(), config.getTelegram().getValue()));

            findViewById(R.id.layout_website).setOnClickListener(v -> // website
                    openBrowser(v.getContext(), config.getOfficial_website().getValue()));

//            findViewById(R.id.layout_gmail).setOnClickListener(v -> // gmail
//                    openEmail(v.getContext(), config.getGmail().getValue()));
        }

    }

    private void openEmail(Context context, String to) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setData(Uri.parse("mailto:" + to));
//        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.setType("text/plain");
        //intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        context.startActivity(Intent.createChooser(intent, ""));
    }

    private void openBrowser(Context context, String url) {
        final Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(Intent.createChooser(intent, "请选择浏览器"));
            finish();
        } else {
            ToastUtils.makeText("链接错误");
        }
    }

}
